﻿using AIVision.PLC;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System.Collections.ObjectModel;
using AIVision.Repository.Services;
using AIVision.Model.MachineModels;
using AIVision.Model.EntityModels;
using System;
using System.Diagnostics;
using AIVision.Model.UIModels;
using Prism.Regions;
using System.Windows;
using AIVision.Model.InspectModels;

namespace AIVision.UI.ViewModels
{
    public class ManualCtrlViewModel:BindableBase
    {
        private string viewName;
        private readonly PLC.Machine machine;
        private readonly PLCConfigService plcConfigService;
        private readonly SummaryDataModel summaryDataModel;
        private readonly InspectStatusModel inspectStatus;
        private readonly InpsectionRecordService inpsectionRecordService;

        public DelegateCommand<string> BtnCommand { get; private set; }
        private PLCConfigDto plcConfig;
        public PLCConfigDto PLCConfig
        {
            get { return plcConfig; }
            set { plcConfig = value; RaisePropertyChanged(); }
        }

        public ObservableCollection<RunMsgModel> RunMsgList { get; set; } = new ObservableCollection<RunMsgModel>();

        public string ViewName
        {
            get { return viewName; }
            set { viewName = value; }
        }
        private MachineStatusModel machineStatus;

        public MachineStatusModel MachineStatus
        {
            get { return machineStatus; }
            set { machineStatus = value;RaisePropertyChanged(); }
        }

        public ManualCtrlViewModel(PLC.Machine machine,MachineStatusModel machineStatus, IEventAggregator eventAggregator,
            PLCConfigService plcConfigService, SummaryDataModel summaryDataModel, InspectStatusModel inspectStatus, InpsectionRecordService inpsectionRecordService)
        {
            this.machine = machine;
            MachineStatus = machineStatus;
            this.plcConfigService = plcConfigService;
            this.summaryDataModel = summaryDataModel;
            this.inspectStatus = inspectStatus;
            this.inpsectionRecordService = inpsectionRecordService;
            ViewName = "手动控制";
            BtnCommand = new DelegateCommand<string>(BtnMethod);
            eventAggregator.GetEvent<PLCRunMsgEvent>().Subscribe(SubscribePLCRunMsg,ThreadOption.UIThread);
            PLCConfig = plcConfigService.GetPLCConfig();
            machine.InitConnection(PLCConfig);
         }

        private void SubscribePLCRunMsg(RunMsgModel msg)
        {
            RunMsgList.Add(msg);
            if(msg.MsgColor == "Red")
                MessageBox.Show(msg.Msg);
            Trace.WriteLine(msg.Msg);
        }
        private void BtnMethod(string cmd)
        {
            switch(cmd)
            {
               
                case "InitZhuanTai": machine.ZhuanTai.ResetLocationAsync(); break;
                case "StepZhuanTai": 
                    machine.ZhuanTai.Run(); 
                   // inspectStatus.VisionPhotoReady = true; 
                    break;
                case "InitPPU": machine.PPU.ResetLocationAsync(); break;
                case "StepPPU": machine.PPU.StepAsync(); break;
                case "InitTuLun": machine.TuLun.ResetLocationAsync(); break;
                case "StepTuLun": machine.TuLun.StepAsync(); break;
                case "InitChaHeX": machine.ChaHe.ResetXLocationAsync(); break;
                case "StepChaHeX": machine.ChaHe.ChaHeXStepAsync(); break;
                case "InitChaHeY": machine.ChaHe.ResetYLocationAsync(); break;
                case "StepChaHeY": machine.ChaHe.ChaHeYStepAsync(); break;
                case "InitFeiLiao": machine.FeiLiao.ResetLocationAsync(); break;
                case "StepFeiLiao": machine.FeiLiao.RunAsync(); break;
                //case "InitConnection": machine.InitConnection(PLCConfig); break;
                case "ResetLocation": machine.ResetLocation();break;
                case "CompleteInspect":machine.CompleteInspect();break;
                case "ClosedBuzzer": machine.ClosedBuzzer(); break;
                case "AutoStartStop":
                    if (machineStatus.AutoRunStatus)
                        machine.AutoRunStart();
                    else
                        machine.AutoRunStop();
                    break;
            }
        }
        private void SavePLCConfig()
        {
            plcConfigService.SavePLCConfig(PLCConfig);
        }

        
    }
}
